<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="chargeActivityId">
        <el-input
          v-model="queryParams.chargeActivityId"
          placeholder="请输入活动ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="couponsId">
        <el-input
          v-model="queryParams.couponsId"
          placeholder="请输入卡券ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="couponsName">
        <el-input
          v-model="queryParams.couponsName"
          placeholder="请输入卡券名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="couponsType">
        <el-input
          v-model="queryParams.couponsType"
          placeholder="请输入卡券类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="phoneNum">
        <el-input
          v-model="queryParams.phoneNum"
          placeholder="请输入持有人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="writeOffStatus">
        <el-input
          v-model="queryParams.writeOffStatus"
          placeholder="请输入核销状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="couponsType">
        <el-input
          v-model="queryParams.couponsType"
          placeholder="请输入卡券状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

<!--      <el-form-item label="现金部分" prop="cashPart">
        <el-input
          v-model="queryParams.cashPart"
          placeholder="请输入现金部分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现金锁定部分" prop="cashPartLocked">
        <el-input
          v-model="queryParams.cashPartLocked"
          placeholder="请输入现金锁定部分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="consumerId">
        <el-input
          v-model="queryParams.consumerId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放券生成的唯一编码" prop="couponsCode">
        <el-input
          v-model="queryParams.couponsCode"
          placeholder="请输入发放券生成的唯一编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="折扣比例 %" prop="discount">
        <el-input
          v-model="queryParams.discount"
          placeholder="请输入折扣比例 %"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-input
          v-model="queryParams.expireTime"
          placeholder="请输入过期时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大抵扣额" prop="maxDeduction">
        <el-input
          v-model="queryParams.maxDeduction"
          placeholder="请输入最大抵扣额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付金额" prop="payment">
        <el-input
          v-model="queryParams.payment"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用时间" prop="usedTime">
        <el-input
          v-model="queryParams.usedTime"
          placeholder="请输入使用时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="虚拟部分" prop="virPart">
        <el-input
          v-model="queryParams.virPart"
          placeholder="请输入虚拟部分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="虚拟锁定部分" prop="virPartLocked">
        <el-input
          v-model="queryParams.virPartLocked"
          placeholder="请输入虚拟锁定部分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销时间" prop="writeOffTime">
        <el-input
          v-model="queryParams.writeOffTime"
          placeholder="请输入核销时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activitys:consumerCoupons:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activitys:consumerCoupons:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activitys:consumerCoupons:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activitys:consumerCoupons:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consumerCouponsList" @selection-change="handleSelectionChange">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="卡券编码" align="center" prop="couponsCode" >
        <template v-slot="scope">
          <el-link @click="onCopy(scope.row.couponsCode)" type="primary">复制</el-link>
        </template>
      </el-table-column>
      <el-table-column label="卡券名称" align="center" prop="couponsName" show-overflow-tooltip/>
      <el-table-column label="卡券类型" align="center" prop="couponsType" />
      <el-table-column label="持有人手机号" align="center" prop="phoneNum" />
      <el-table-column label="核销状态" align="center" prop="writeOffStatus" >
        <!--    (0:创建1:锁定2:核销3:过期失效)    -->
        <template slot-scope="scope">
          {{writeOffStatusList[scope.row.writeOffStatus]}}
        </template>
      </el-table-column>
      <el-table-column label="核销时间" align="center" prop="writeOffTime">
        <template slot-scope="scope">
          {{ parseTime(scope.row.writeOffTime) }}
        </template>
      </el-table-column>
      <el-table-column label="购买时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="expireTime" >
        <template slot-scope="scope">
          {{scope.row.expireTime===0?'无限':parseTime(scope.row.expireTime)}}
        </template>
      </el-table-column>
      <el-table-column label="卡券状态" align="center" prop="couponsStatus" >
        <!--    (0:创建1:锁定2:核销3:过期失效)    -->
        <template slot-scope="scope">
          {{couponsStatusList[scope.row.couponsStatus]}}
        </template>
      </el-table-column>

<!--      <el-table-column label="现金部分" align="center" prop="cashPart" />
      <el-table-column label="现金锁定部分" align="center" prop="cashPartLocked" />
      <el-table-column label="活动ID" align="center" prop="chargeActivityId" />
      <el-table-column label="用户ID" align="center" prop="consumerId" />-->



<!--      <el-table-column label="折扣比例 %" align="center" prop="discount" />
      <el-table-column label="最大抵扣额" align="center" prop="maxDeduction" />
      <el-table-column label="支付类型" align="center" prop="payType" />
      <el-table-column label="支付金额" align="center" prop="payment" />
      <el-table-column label="支付状态" align="center" prop="status" >
        &lt;!&ndash;    (0 未支付 1已支付)    &ndash;&gt;
        <template slot-scope="scope">
          {{scope.row.status===0?'未支付':'已支付'}}
        </template>
      </el-table-column>
      <el-table-column label="使用时间" align="center" prop="usedTime" />
      <el-table-column label="虚拟部分" align="center" prop="virPart" />
      <el-table-column label="虚拟锁定部分" align="center" prop="virPartLocked" />-->


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activitys:consumerCoupons:edit']"
          >详情</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activitys:consumerCoupons:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户参与活动卡券对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-descriptions class="margin-top" title="活动" :column="4" :size="size" border>
        <el-descriptions-item>
          <template slot="label">
            卡券名称
          </template>
          {{form.activity.activityName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            别名
          </template>
          {{form.activity.activityNickName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            状态
          </template>
          {{form.activity.status===0?'未支付':'已支付'}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            活动ID
          </template>
          <el-tag size="small">{{form.activity.id}}</el-tag>
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions class="margin-top" style="margin-top: 10px" title="用户详情" :column="4" :size="size" border>
        <el-descriptions-item>
          <template slot="label">
            用户名称
          </template>
          {{form.consumer.nickName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            手机号
          </template>
          <el-tag size="small">{{form.consumer.phoneNum}}</el-tag>
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions class="margin-top" style="margin-top: 10px" title="卡券" :column="4" :size="size" border>
        <el-descriptions-item>
          <template slot="label">
            卡券名称
          </template>
          {{form.coupons.couponsName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            卡券类型
          </template>
          <el-cascader
            disabled
            v-model="form.coupons.couponsSubType"
            :options="couponsTypeList"
            style="

            width: 200px"
          ></el-cascader>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            购买时间
          </template>
          {{ parseTime(form.consumerCouponsList.createTime) }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            过期时间
          </template>
          {{form.consumerCouponsList.expireTime===0?'无限':parseTime(form.consumerCouponsList.expireTime)}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            有效时间
          </template>
          {{form.consumerCouponsList.effectiveSecond}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            支付类型
          </template>
          {{form.consumerCouponsList.payType}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            支付金额
          </template>
          {{form.consumerCouponsList.payment}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            支付状态
          </template>
          {{form.consumerCouponsList.status==0?'未支付':'已支付'}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            现金余额
          </template>
          {{form.consumerCouponsList.cashPart}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            虚拟余额
          </template>
          {{form.consumerCouponsList.virPart}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            售价
          </template>
          {{form.coupons.price}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            最大支付抵扣额
          </template>
          {{form.coupons.maxDeduction}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            折扣比例
          </template>
          {{form.coupons.discount}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            触发阈值
          </template>
          {{form.coupons.minThreshold}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            核销类型
          </template>
          {{form.coupons.writeOffType}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            核销状态
          </template>
          {{writeOffStatusList[form.coupons.writeOffStatus]}}
        </el-descriptions-item>

      </el-descriptions>
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="现金部分" prop="cashPart">
          <el-input v-model="form.cashPart" placeholder="请输入现金部分" />
        </el-form-item>
        <el-form-item label="现金锁定部分" prop="cashPartLocked">
          <el-input v-model="form.cashPartLocked" placeholder="请输入现金锁定部分" />
        </el-form-item>
        <el-form-item label="活动ID" prop="chargeActivityId">
          <el-input v-model="form.chargeActivityId" placeholder="请输入活动ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="consumerId">
          <el-input v-model="form.consumerId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="发放券生成的唯一编码" prop="couponsCode">
          <el-input v-model="form.couponsCode" placeholder="请输入发放券生成的唯一编码" />
        </el-form-item>
        <el-form-item label="卡券ID" prop="couponsId">
          <el-input v-model="form.couponsId" placeholder="请输入卡券ID" />
        </el-form-item>
        <el-form-item label="折扣比例 %" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入折扣比例 %" />
        </el-form-item>
        <el-form-item label="过期时间" prop="expireTime">
          <el-input v-model="form.expireTime" placeholder="请输入过期时间" />
        </el-form-item>
        <el-form-item label="最大抵扣额" prop="maxDeduction">
          <el-input v-model="form.maxDeduction" placeholder="请输入最大抵扣额" />
        </el-form-item>
        <el-form-item label="支付金额" prop="payment">
          <el-input v-model="form.payment" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="使用时间" prop="usedTime">
          <el-input v-model="form.usedTime" placeholder="请输入使用时间" />
        </el-form-item>
        <el-form-item label="虚拟部分" prop="virPart">
          <el-input v-model="form.virPart" placeholder="请输入虚拟部分" />
        </el-form-item>
        <el-form-item label="虚拟锁定部分" prop="virPartLocked">
          <el-input v-model="form.virPartLocked" placeholder="请输入虚拟锁定部分" />
        </el-form-item>
        <el-form-item label="核销时间" prop="writeOffTime">
          <el-input v-model="form.writeOffTime" placeholder="请输入核销时间" />
        </el-form-item>
        <el-form-item label="卡券名称" prop="couponsName">
          <el-input v-model="form.couponsName" placeholder="请输入卡券名称" />
        </el-form-item>
      </el-form>-->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConsumerCoupons, getConsumerCoupons, delConsumerCoupons, addConsumerCoupons, updateConsumerCoupons } from "@/api/business/consumerCoupons";
import {findAllCouponsType} from "@/api/business/coupons";
const couponsStatusList = ['创建','锁定','核销','过期失效']
const writeOffStatusList = ['创建','锁定','核销','过期失效']
export default {
  name: "ConsumerCoupons",
  data() {
    return {
      couponsStatusList,
      writeOffStatusList,
      size: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户参与活动卡券表格数据
      consumerCouponsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cashPart: null,
        cashPartLocked: null,
        chargeActivityId: null,
        consumerId: null,
        couponsCode: null,
        couponsId: null,
        couponsStatus: null,
        discount: null,
        expireTime: null,
        maxDeduction: null,
        payType: null,
        payment: null,
        status: null,
        usedTime: null,
        virPart: null,
        virPartLocked: null,
        writeOffStatus: null,
        writeOffTime: null,
        phoneNum: null,
        couponsName: null,
        couponsType: null
      },
      // 表单参数
      form: {
        activity:{},
        consumer:{},
        consumerCouponsList:{},
        coupons:{},
      },
      // 表单校验
      rules: {
      },
      couponsTypeList: [],
      couponsTypeShow:[],
    };
  },
  created() {
    this.getList();
    this.getFindAllCouponsType()
  },
  methods: {
    async getFindAllCouponsType() {
      let res = await findAllCouponsType()
      this.couponsTypeList = res.data
    },
    onCopy(val) {
      this.$copyText(val).then(
        e => {
          this.$message({
            message: '复制成功',
            type: 'success'
          })
        },
        e => {
          console.log('复制失败：', e);
        }
      )
    },
    /** 查询用户参与活动卡券列表 */
    getList() {
      this.loading = true;
      listConsumerCoupons(this.queryParams).then(response => {
        this.consumerCouponsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activity:{},
        consumer:{},
        consumerCouponsList:{},
        coupons:{},
        /*id: null,
        cashPart: null,
        cashPartLocked: null,
        chargeActivityId: null,
        consumerId: null,
        couponsCode: null,
        couponsId: null,
        couponsStatus: null,
        createTime: null,
        discount: null,
        expireTime: null,
        maxDeduction: null,
        payType: null,
        payment: null,
        status: null,
        usedTime: null,
        virPart: null,
        virPartLocked: null,
        writeOffStatus: null,
        writeOffTime: null,
        couponsName: null,
        couponsType: null*/
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户参与活动卡券";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConsumerCoupons(id).then(response => {
        this.form = response.data;
        this.couponsTypeShow = [this.form.coupons.couponsType,this.form.coupons.couponsSubType]
        this.open = true;
        this.title = "修改用户参与活动卡券";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConsumerCoupons(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsumerCoupons(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户参与活动卡券编号为"' + ids + '"的数据项？').then(function() {
        return delConsumerCoupons(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activitys/consumerCoupons/export', {
        ...this.queryParams
      }, `consumerCoupons_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
